home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 3_0 / DOUBLEDE / WINDOWS.C < prev    next >
C/C++ Source or Header  |  1988-07-28  |  11KB  |  459 lines

  1. #include "shell.h"
  2.  
  3. /*====================================================================
  4.  
  5.     makewindows()
  6.     
  7.     create some application windows.  "windows" is a global WindowPtr
  8.     array for general application use.  the window definition proc
  9.     is defined in shell.h.  clip_window is a window to display the
  10.     contents of the clipboard.
  11.     
  12.     global definition NUMWINDOWS (shell.h) indicates how many
  13.     application windows to create
  14.     
  15. ====================================================================*/    
  16. makewindows()
  17. {
  18.     #define        WINDOWWIDTH        160
  19.  
  20.     Rect        wRect;
  21.     Rect        cRect;
  22.     int            index;
  23.     Str255        wTitle;
  24.     Str255        aStr;
  25.  
  26.     wRect = screenBits.bounds;
  27.     wRect.top = wRect.bottom - 80;
  28.     InsetRect(&wRect,12,12);
  29.     SetPort(clip_window = NewWindow(NIL,&wRect,"\pClipboard",FALSE,8,-1L,TRUE,0));
  30.     TextFont(4);
  31.     TextSize(9);
  32.     centerwindow(clip_window,&wRect);
  33.  
  34.     SetRect(&cRect,0,0,1,1);
  35.     SetRect(&wRect,10,50,180,200);
  36.     for(index=0; index<NUMWINDOWS; index++){
  37.         /*
  38.             build window title from index
  39.         */
  40.         strcpy(wTitle,"Untitled ");
  41.         NumToString((long)index+1L,aStr);
  42.         PtoCstr((char *)aStr);
  43.         strcat(wTitle,aStr);
  44.         CtoPstr((char *)wTitle);
  45.         
  46.         /*
  47.             make & show the window(s)
  48.         */
  49.         SetPort(windows[index] = NewWindow(NIL,&wRect,wTitle,FALSE,WINDOWDEFPROC,-1L,TRUE,0));
  50.         TextFont(4);
  51.         TextSize(9);
  52.         TextFace(0);
  53.         TextMode(srcCopy);
  54.         ShowWindow(windows[index]);
  55.         
  56.         demoButton[index] = NewControl(windows[index],&cRect,"\pDemo",TRUE,0,0,1,0,0);
  57.         posbutton(index);
  58.         
  59.         demo_double[index] = 3.14159265;
  60.         
  61.         /*
  62.             offset the rectangle for the next window
  63.         */
  64.         OffsetRect(&wRect,16,16);
  65.     }
  66. }
  67.  
  68. /*====================================================================
  69.  
  70.     handleactivate()
  71.     
  72.     called on an activate/deactivate event.
  73.     
  74. ====================================================================*/    
  75. handleactivate()
  76. {
  77.     WindowPtr        eventWindow;
  78.  
  79.     SetPort(eventWindow = (WindowPtr)event.message);
  80.     
  81.     /*
  82.         is it one of our windows?
  83.     */
  84.     if ((w_index = ourwindow(eventWindow)) >= 0){
  85.         
  86.         /*
  87.             DrawGrowIcon(WindowPtr) will correctly draw the
  88.             grow icon whether the window is being activated
  89.             or deactivated
  90.         */
  91.         
  92.         DrawGrowIcon(eventWindow);
  93.         
  94.         if (BitAnd(event.modifiers,activeFlag != 0)){
  95.         
  96.             /*    what to do on activate        */
  97.  
  98.             HiliteControl(demoButton[w_index],ACTIVE);
  99.             
  100.             if (BitAnd(event.modifiers,changeFlag) != 0){
  101.                 
  102.                 /*
  103.                     what to do when coming from a DA
  104.                     or coming from another application
  105.                     running under MultiFinder
  106.                 */
  107.  
  108.             }
  109.         }
  110.         else{
  111.         
  112.             /*     what to do on deactivate    */
  113.  
  114.             HiliteControl(demoButton[w_index],INACTIVE);
  115.             
  116.             if (BitAnd(event.modifiers,changeFlag) != 0){
  117.             
  118.                 /*
  119.                     what to do when leaving to a DA
  120.                     or going to another application
  121.                     running under MultiFinder
  122.                 */
  123.                 
  124.             }
  125.         }
  126.     }
  127.  
  128.     if ( (eventWindow==clip_window) ){
  129.         /*
  130.             window involved is our clipboard window
  131.         */
  132.         
  133.         DrawGrowIcon(eventWindow);
  134.         
  135.         if (BitAnd(event.modifiers,activeFlag != 0)){
  136.             /*
  137.                 what to do if clipboard is being made active,
  138.                 clipboard changes are handled elsewhere from
  139.                 selections made in the "Edit" menu
  140.             */
  141.         }
  142.         else{
  143.             /*
  144.                 what to do if clipboard is being deactivated
  145.             */
  146.         }
  147.     }
  148. }
  149.  
  150. /*====================================================================
  151.  
  152.     handleupdate(WindowPtr)
  153.     
  154.     called on an update event.  If it is one of our application
  155.     windows, clip drawing to inside the scroll bar areas, and do
  156.     whatever drawing is needed.
  157.     
  158.     if clipboard window needs updating, call updateclipboard()
  159.     
  160. ====================================================================*/    
  161. handleupdate(thisPort)
  162. GrafPtr        thisPort;
  163. {
  164.     GrafPtr            tempPort;
  165.     Rect            dummyRect;
  166.     Str255            aStr;
  167.  
  168.     GetPort(&tempPort);
  169.     SetPort(thisPort);
  170.  
  171.  
  172.     if ((w_index = ourwindow(thisPort)) >= 0){
  173.  
  174.         BeginUpdate(thisPort);
  175.         EraseRect(&thisPort->portRect);
  176.         DrawGrowIcon(thisPort);
  177.         DrawControls(thisPort);
  178.  
  179.         /*
  180.             clip drawing to _exclude_ the scroll bar areas, and the
  181.             grow icon area
  182.         */
  183.         dummyRect = thisPort->portRect;
  184.         dummyRect.right     -= SCROLLBARWIDTH;
  185.         dummyRect.bottom -= SCROLLBARWIDTH;
  186.         ClipRect(&dummyRect);
  187.         
  188.         /*
  189.             our drawing routine....
  190.         */
  191.         strcpy(aStr,"\pValue = ");
  192.         MoveTo(4,15);
  193.         DrawString(aStr);
  194.         double_2_cString(&demo_double[w_index],FIXEDDECIMAL,8,aStr);
  195.         DrawText(aStr,0,strlen(aStr));
  196.         
  197.         /*
  198.             reset clipping to an arbitrarily large rectangle
  199.         */
  200.         SetRect(&dummyRect,-32700,-32700,32700,32700);
  201.         ClipRect(&dummyRect);
  202.  
  203.         EndUpdate(thisPort);
  204.     }
  205.  
  206.     if (thisPort==clip_window)
  207.         updateclipboard();
  208.  
  209.     SetPort(tempPort);
  210. }
  211.  
  212. /*====================================================================
  213.  
  214.     drawGIcon(WindowPtr)
  215.     
  216.     draws only the grow icon, not the scroll bar lines.  clips
  217.     drawing to the rectangle of the grow icon, then calls
  218.     DrawGrowIcon(WindowPtr)
  219.     
  220. ====================================================================*/    
  221. drawGIcon(theWindow)
  222. WindowPtr        theWindow;
  223. {
  224.     RgnHandle        oldClip;
  225.     GrafPtr            tempPort;
  226.     Rect            r;
  227.  
  228.     GetPort(&tempPort);
  229.     SetPort(theWindow);
  230.  
  231.     /*
  232.         save the current clip region in "oldClip"
  233.     */
  234.     oldClip = NewRgn();
  235.     GetClip(oldClip);
  236.  
  237.     /*
  238.         clip drawing to grow icon area
  239.     */
  240.     r = theWindow->portRect;
  241.     r.top = r.bottom - 15;
  242.     r.left = r.right - 15;
  243.     ClipRect(&r);
  244.     DrawGrowIcon(theWindow);
  245.  
  246.     /*
  247.         restore previous clipping, and dispose local RgnHandle
  248.     */
  249.     SetClip(oldClip);
  250.     DisposeRgn(oldClip);
  251.  
  252.     SetPort(tempPort);
  253. }
  254.  
  255. /*====================================================================
  256.  
  257.     dodrag(Point,WindowPtr)
  258.     
  259.     drag a window around in the screen_rect.
  260.     
  261. ====================================================================*/
  262. dodrag(aPoint,eventWindow)
  263. Point            aPoint;
  264. WindowPtr        eventWindow;
  265. {
  266.     /*
  267.         check to see if window is not frontmost, and that the command
  268.         key is not down.  If both of those are true, then bring the
  269.         window to front via SelectWindow(WindowPtr).  If the window
  270.         is not frontmost and the command key _is_ down, then we can't
  271.         bring it to front since command dragging should not select the
  272.         window
  273.     */
  274.     if ( (FrontWindow() != eventWindow) && ((event.modifiers & cmdKey) == 0) )
  275.         SelectWindow(eventWindow);
  276.     else
  277.         DragWindow(eventWindow, aPoint, &screen_rect);
  278. }
  279.  
  280. /*====================================================================
  281.  
  282.     docontent(Point,WindowPtr)
  283.     
  284.     a click occurred in the window indicated in the WindowPtr parameter.
  285.     the Point is in global coordinates, and must be converted to local
  286.     window coordinates to determine what area of the window content was
  287.     hit.
  288.     
  289. ====================================================================*/
  290. docontent(aPoint,eventWindow)
  291. Point            aPoint;
  292. WindowPtr        eventWindow;
  293. {
  294.     ControlHandle    theControl;
  295.     int                Part;
  296.  
  297.     if (FrontWindow() != eventWindow)
  298.         SelectWindow(eventWindow);
  299.     else{
  300.         GlobalToLocal(&aPoint);
  301.         if ((w_index = ourwindow(eventWindow)) >= 0){
  302.             Part = FindControl(aPoint,eventWindow,&theControl);
  303.             if (theControl == demoButton[w_index]){
  304.                 if (Part = inButton){
  305.                     Part = TrackControl(theControl,aPoint,NIL);
  306.                     if (Part == inButton){
  307.                         do_demo(w_index);
  308.                     }
  309.                 }
  310.             }
  311.         }
  312.     }
  313. }
  314.  
  315. /*====================================================================
  316.  
  317.     InvalHScrollArea(WindowPtr)
  318.     
  319.     Invalidates the horizontal scroll bar area of the window indicated
  320.     by the WindowPtr parameter.  forces an update event.
  321.     
  322. ====================================================================*/
  323. InvalHScrollArea(whichWindow)
  324. WindowPtr    whichWindow;
  325. {
  326.     GrafPtr        tp;
  327.     Rect        r;
  328.  
  329.     GetPort(&tp);
  330.     SetPort(whichWindow);
  331.     r = whichWindow->portRect;
  332.     r.top = r.bottom - SCROLLBARWIDTH;
  333.     InvalRect(&r);
  334.     SetPort(tp);
  335. }
  336.  
  337. /*====================================================================
  338.  
  339.     InvalVScrollArea(WindowPtr)
  340.     
  341.     Invalidates the vertical scroll bar area of the window indicated
  342.     by the WindowPtr parameter.  forces an update event.
  343.     
  344. ====================================================================*/
  345. InvalVScrollArea(whichWindow)
  346. WindowPtr    whichWindow;
  347. {
  348.     GrafPtr        tp;
  349.     Rect        r;
  350.  
  351.     GetPort(&tp);
  352.     SetPort(whichWindow);
  353.     r = whichWindow->portRect;
  354.     r.left = r.right - SCROLLBARWIDTH;
  355.     InvalRect(&r);
  356.     SetPort(tp);
  357. }
  358.  
  359. /*====================================================================
  360.  
  361.     dogrow(Point,WindowPtr)
  362.     
  363.     resize the window indicated by the WindowPtr parameter.
  364.     
  365. ====================================================================*/
  366. dogrow(aPoint,eventWindow)
  367. Point            aPoint;
  368. WindowPtr        eventWindow;
  369. {
  370.     register int    index;
  371.     int                newWidth;
  372.     int                newHeight;
  373.     long            newSize;
  374.     Rect            limitRect;
  375.  
  376.     if (FrontWindow() != eventWindow)
  377.         SelectWindow(eventWindow);
  378.     else{
  379.         if ((w_index = ourwindow(eventWindow)) >= 0){
  380.                 /*
  381.                     smallest size is 80h x 40v,
  382.                     largest size is arbitrarily large
  383.                 */
  384.                 SetRect(&limitRect,80,40,32000,32000);
  385.                 newSize = GrowWindow(eventWindow,aPoint,&limitRect);
  386.                 if (newSize != NIL){
  387.                     newWidth = LoWord(newSize);
  388.                     newHeight = HiWord(newSize);
  389.                     InvalHScrollArea(eventWindow);
  390.                     InvalVScrollArea(eventWindow);
  391.                     SizeWindow(eventWindow,newWidth,newHeight,TRUE);
  392.                     InvalRect(&eventWindow->portRect);
  393.                     posbutton(w_index);
  394.                 }
  395.         }
  396.  
  397.         if (eventWindow == clip_window){
  398.                 SetRect(&limitRect,80,40,32000,32000);
  399.                 newSize = GrowWindow(eventWindow,aPoint,&limitRect);
  400.                 if (newSize != NIL){
  401.                     InvalHScrollArea(eventWindow);
  402.                     InvalVScrollArea(eventWindow);
  403.                     newWidth = LoWord(newSize);
  404.                     newHeight = HiWord(newSize);
  405.                     SizeWindow(eventWindow,newWidth,newHeight,TRUE);
  406.                     InvalRect(&eventWindow->portRect);
  407.                 }
  408.         }
  409.     }
  410. }
  411.  
  412. /*====================================================================
  413.  
  414.     dogoaway(Point,WindowPtr)
  415.     
  416.     user clicked in the goaway box of one of our windows, track the
  417.     click to allow them to change their minds before releasing the
  418.     mouse button
  419.     
  420.     if global boolean "quit_on_wind_close" is TRUE, then this will
  421.     cause the application to quit.
  422.     
  423. ====================================================================*/
  424. dogoaway(aPoint,eventWindow)
  425. Point            aPoint;
  426. WindowPtr        eventWindow;
  427. {
  428.     if (TrackGoAway(eventWindow,aPoint)){
  429.         if ((w_index = ourwindow(eventWindow)) >= 0){
  430.                 quitting = quit_on_wind_close;
  431.                 HideWindow(eventWindow);
  432.         }
  433.     }
  434.  
  435.         if (eventWindow==clip_window)
  436.             showclipwindow();
  437. }
  438.  
  439. /*====================================================================
  440.  
  441.     ourwindow(WindowPtr)
  442.     
  443.     if the window indicated by the WindowPtr parameter is in our
  444.     array of application windows, returns the index into the array
  445.     for that window, from 0..NUNWINDOWS-1.
  446.     
  447.     if the window is not in the array, returns -1
  448.     
  449. ====================================================================*/
  450. ourwindow(theWindow)
  451. WindowPtr        theWindow;
  452. {
  453.     register int        index;
  454.  
  455.     for(index=0;index<NUMWINDOWS;index++)
  456.         if (theWindow==windows[index])
  457.             return(index);
  458.     return(-1);
  459. }